<?php

class CpmanufacturersController extends Core_Controller_Action 
{
	private $_manufacturers;
	
	public function init() {
	
		$this->_manufacturers = new Manufacturers();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cpmanufacturers' );
	}
	
	public function itemAction() {
	
		$this->view->fileTypes = '"*.jpg"';
		$this->view->fileTypesDescription = 'Tylko zdjęcia';
		$this->view->uploadSuccess = 'uploadSuccess';
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );

		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			if( empty( $post['name_pl'] ) ) {
				$this->view->err_name_pl = 'Podaj nazwe';
			} else {

				if( $id ) {
					$item = $this->_manufacturers->getItem( $id )->getFirst();

					if( $item->logo != $post['logo'] ) {
						$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['logo'] );
						$images->thumb( 170, 170 );
						
						$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['logo'] );
						$images->thumb( 200, 200 );
					}

					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$item->{$key} = $value;
						}
					}

					$item->save();

					$this->_redirect( 'cpmanufacturers/list/oper/2' );
				} else {
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$this->_manufacturers->{$key} = $value;
						}
					}
					
					if( !empty( $this->_manufacturers->logo ) ) {
						$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $this->_manufacturers->logo );
						$images->thumb( 170, 170 );
						
						$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $this->_manufacturers->logo );
						$images->thumb( 200, 200 );
					}
					
					$this->_manufacturers->save();
					
					$this->_redirect( 'cpmanufacturers/list/oper/1' );
				}
			}

		} else {
			if( $id ) {
				$item = $this->_manufacturers->getItem( $id )->toArray();

				foreach( $item[0] as $key => $value ) {
	
					$this->view->{$key} = $value;
				}
			}
		}

		$this->assignType( 0 );
	}
	
	public function listAction() {
		if( $this->_request->isPost() ) {
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();
			
			$post = $this->_request->getPost();
			
			$i = 1;
			foreach( $post['data'] as $value ) {
				$Manufacturers = new Manufacturers();
				$item = $Manufacturers->getItem( $value['name'] )->getFirst();
				$item->order_item = $i;
				$item->save();
				
				$i++;
			}

			echo Zend_Json::encode( array( 'status' => 1 ) );
		} else {
			$page = intval( $this->_request->getParam( 'page', 1 ) );
	
			$pagerLayout = $this->_manufacturers->getListWithPager( $page, $this->view->baseUrl() );
	
			$this->view->list = $pagerLayout->execute();
			$this->view->pager = $pagerLayout;
	
			$this->assignType( 0 );
		}
	}
	
	public function productsAction() {
		$id = $this->_request->getParam( 'id' );
		
		if( $this->_request->isPost() ) {
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();
			
			$post = $this->_request->getPost();

			$i = 1;
			foreach( $post['data'] as $value ) {
				$Products = new Products();
				$item = $Products->getItem( $value['name'] )->getFirst();
				$item->order_item = $i;
				$item->save();
				
				$i++;
			}

			echo Zend_Json::encode( array( 'status' => 1 ) );
		} else {
			$Products = new Products();

			$this->view->list = $Products->getListByProducer( $id );
	
			$this->assignType( 0 );
		}
	}
	
	public function deleteAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$this->_manufacturers->deleteItem( $id );
			
			$this->_redirect( 'cpmanufacturers/list/oper/4' );
		} else {
			$this->_redirect( 'cpmanufacturers/list/oper/3' );
		}
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 0 :
				$this->view->yih2 = 'Producenci';
				break;
		}
	}
}